﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wsitech.BLL;
using Wsitech.Model;

public partial class Backend_CompanyAdd : BackendPageBase
{
    protected readonly string companyid = HttpContext.Current.Request["companyid"] ?? "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (companyid != "")
            {
                initCompany(companyid);
            }
        }
    }

    private void initCompany(string companyid)
    {
        CompanyInfo company = CompanyInfoBLL.Instance.GetDetail(companyid);
        if (company != null)
        {
            txtCompanyName.Text = company.CompanyName;
            txtEnName.Text = company.EnCompanyName;
            txtFullName.Text = company.FullName;
            txtDescription.Text = company.Description;
            txtIndexUrl.Text = company.IndexUrl;
            txtJobUrl.Text = company.JobUrl;
        }
    }


    //添加或修改
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (CheckParams())
        {
            if (companyid == "")
            {
                //添加公司信息
                CompanyInfoBLL.Instance.AddCompany(
                    txtCompanyName.Text.Trim(),
                    txtEnName.Text.Trim(),
                    txtFullName.Text.Trim(),
                    txtDescription.Text.Trim(),
                    txtIndexUrl.Text.Trim(),
                    txtJobUrl.Text.Trim()
                    );
            }
            else
            {
                CompanyInfo company = CompanyInfoBLL.Instance.GetDetail(companyid);
                if (company != null)
                {
                    company.CompanyName = txtCompanyName.Text.Trim();
                    company.EnCompanyName = txtEnName.Text.Trim();
                    company.FullName = txtFullName.Text.Trim();
                    company.Description = txtDescription.Text.Trim();
                    company.IndexUrl = txtIndexUrl.Text.Trim();
                    company.JobUrl = txtJobUrl.Text.Trim();

                    CompanyInfoBLL.Instance.Update(company);
                }
            }
            base.RefreshParent();
        }
    }

    //数据检查
    private bool CheckParams()
    {
        if (txtCompanyName.Text.Trim() == "")
        {
            base.Alert("公司名称不能为空！");
            return false;
        }
        else if (txtCompanyName.Text.Trim().Length > 7)
        {
            base.Alert("公司名称不超过7个字");
            return false;
        }
        if (txtEnName.Text.Trim() == "")
        {
            base.Alert("英文名称不能为空");
            return false;
        }
        else if (txtEnName.Text.Trim().Length > 50)
        {
            base.Alert("英文名称不超过50个字符");
            return false;
        }
        if (txtFullName.Text.Trim() == "")
        {
            base.Alert("公司全称不能为空！");
            return false;
        }
        else if (txtFullName.Text.Trim().Length > 20)
        {
            base.Alert("公司全称不超过20字！");
            return false;
        }
        //if (txtDescription.Text.Trim() == "")
        //{
        //    base.Alert("公司简介不能为空！");
        //    return false;
        //}
        //else 
        if (txtDescription.Text.Trim().Length > 1000)
        {
            base.Alert("公司简介不超过1000字");
            return false;
        }
        return true;
    }
}